package eic.muckup.garden;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class LimitSetupActivity extends Activity {
	private final String myPrefs = "LIMIT_VALUE";
	private SharedPreferences mPrefs;
	private SharedPreferences.Editor editor;
	
	private final String MIN_MOISTURE_VALUE = "minMoisture";
	private final String MAX_MOISTURE_VALUE = "maxMoisture";
	private final String MIN_TEMPERATURE_VALUE = "minTemperature";
	private final String MAX_TEMPERATURE_VALUE = "maxTemperature";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mPrefs = getSharedPreferences(myPrefs, Activity.MODE_PRIVATE);
		editor = mPrefs.edit();
		
		setContentView(R.layout.limit_values_view);
	}
	
	@Override
	protected void onResume() {
		EditText minMoisture = (EditText)findViewById(R.id.MinMoistureEditText);
		EditText maxMoisture = (EditText)findViewById(R.id.MaxMoistureEditText);
		EditText minTempera = (EditText)findViewById(R.id.MinTemeratureEditText);
		EditText maxTempera = (EditText)findViewById(R.id.MaxTemeratureEditText);
		
		minMoisture.setText(String.valueOf(mPrefs.getFloat(MIN_MOISTURE_VALUE, 20)));
		maxMoisture.setText(String.valueOf(mPrefs.getFloat(MAX_MOISTURE_VALUE, 100)));
		minTempera.setText(String.valueOf(mPrefs.getFloat(MIN_TEMPERATURE_VALUE, 0)));
		maxTempera.setText(String.valueOf(mPrefs.getFloat(MAX_TEMPERATURE_VALUE, 100)));		
		
		super.onResume();
	}
	
	public void SaveLimitValues(View view){
		EditText minMoisture = (EditText)findViewById(R.id.MinMoistureEditText);
		EditText maxMoisture = (EditText)findViewById(R.id.MaxMoistureEditText);
		EditText minTempera = (EditText)findViewById(R.id.MinTemeratureEditText);
		EditText maxTempera = (EditText)findViewById(R.id.MaxTemeratureEditText);
		
		Boolean allOK = true;
		
		Float limit;
		if(minMoisture.getText().length() > 0){
			limit = Float.parseFloat(minMoisture.getText().toString().replace(',', '.'));
			if(limit >= 0 && limit <= 100){
				editor.putFloat(MIN_MOISTURE_VALUE, limit);
			}
			else{
				Toast.makeText(this, "Min moisture must be between 0 and 100", Toast.LENGTH_SHORT).show();
				allOK = false;
			}
		}
		
		if(maxMoisture.getText().length() > 0){
			limit = Float.parseFloat(maxMoisture.getText().toString().replace(',', '.'));
			if(limit >= 0 && limit <= 100){
				editor.putFloat(MAX_MOISTURE_VALUE, limit);
			}
			else{
				Toast.makeText(this, "Max moisture must be between 0 and 100", Toast.LENGTH_SHORT).show();
				allOK = false;
			}
		}
		
		if(minTempera.getText().length() > 0){
			limit = Float.parseFloat(minTempera.getText().toString().replace(',', '.'));
			if(limit >= -20 && limit <= 100){
				editor.putFloat(MIN_TEMPERATURE_VALUE, limit);
			}
			else{
				Toast.makeText(this, "Min temperature must be between -20 and 100", Toast.LENGTH_SHORT).show();
				allOK = false;
			}
		}
		
		if(maxTempera.getText().length() > 0){
			limit = Float.parseFloat(maxTempera.getText().toString().replace(',', '.'));
			if(limit >= -20 && limit <= 100){
				editor.putFloat(MAX_TEMPERATURE_VALUE, limit);
			}
			else{
				Toast.makeText(this, "Max temperature must be between -20 and 100", Toast.LENGTH_SHORT).show();
				allOK = false;
			}
		}
		editor.commit();
		if(allOK){
			Toast.makeText(this, "All limits have been saved", Toast.LENGTH_SHORT).show();
		}
		else{
			Toast.makeText(this, "NOT all limits have been saved", Toast.LENGTH_SHORT).show();
		}
	}
}
